package fairygui.editor.publish
{
   import fairygui.editor.animation.AniDef;
   import fairygui.editor.animation.AniFrame;
   import fairygui.editor.animation.AniTexture;
   import fairygui.editor.gui.EPackageItem;
   import fairygui.editor.gui.EUIProject;
   import fairygui.editor.utils.BulkTasks;
   import fairygui.editor.utils.UtilsFile;
   import fairygui.editor.utils.UtilsStr;
   import flash.display.BitmapData;
   import flash.geom.Matrix;
   import flash.utils.ByteArray;
   
   public class CreateOutput extends PublishStep
   {
       
      
      private var _tasks:BulkTasks;
      
      public function CreateOutput()
      {
         super();
      }
      
      override public function run() : void
      {
         var _loc5_:EPackageItem = null;
         var _loc6_:String = null;
         var _loc7_:ByteArray = null;
         var _loc8_:String = null;
         var _loc9_:XML = null;
         var _loc10_:EPackageItem = null;
         var _loc11_:AniDef = null;
         var _loc12_:int = 0;
         var _loc13_:XML = null;
         var _loc14_:XML = null;
         var _loc15_:int = 0;
         var _loc16_:AniFrame = null;
         var _loc17_:XML = null;
         var _loc18_:AniTexture = null;
         var _loc19_:AtlasOutput = null;
         var _loc20_:XML = null;
         var _loc1_:XML = <packageDescription/>;
         _loc1_.@id = publishData.pkg.id;
         _loc1_.@name = publishData.pkg.name;
         var _loc2_:XML = <resources/>;
         var _loc3_:String = publishData._project.settingsCenter.common.verticalScrollBar;
         var _loc4_:String = publishData._project.settingsCenter.common.horizontalScrollBar;
         if(_loc3_ || _loc4_)
         {
            _loc1_.@scrollBarRes = (!!_loc3_?_loc3_:"") + "," + (!!_loc4_?_loc4_:"");
         }
         for each(_loc5_ in publishData.items)
         {
            _loc8_ = UtilsStr.getFileExt(_loc5_.fileName);
            if(_loc8_)
            {
               _loc6_ = _loc5_.id + "." + _loc8_;
            }
            else
            {
               _loc6_ = _loc5_.id;
            }
            _loc9_ = _loc5_.serialize(true);
            if(_loc5_.type == EPackageItem.COMPONENT)
            {
               if(!publishData.outputDesc[_loc5_.id + ".xml"])
               {
                  _loc9_ = null;
               }
            }
            else if(_loc5_.type == EPackageItem.IMAGE)
            {
               if(!publishData.usingAtlas)
               {
                  if(_loc5_.imageInfo.file && (_loc7_ = UtilsFile.loadBytes(_loc5_.imageInfo.file)))
                  {
                     _loc10_ = publishData._fontTextures[_loc5_.id];
                     if(_loc10_)
                     {
                        _loc6_ = _loc10_.id + "." + _loc8_;
                        publishData.outputRes[_loc6_] = _loc7_;
                        _loc9_ = null;
                     }
                     else
                     {
                        publishData.outputRes[_loc6_] = _loc7_;
                        _loc9_.@file = _loc6_;
                     }
                  }
               }
            }
            else if(_loc5_.type == EPackageItem.MOVIECLIP)
            {
               _loc11_ = AniDef(_loc5_.data);
               _loc12_ = _loc11_.frameCount;
               _loc13_ = <movieclip/>;
               _loc13_.@interval = int(1000 / _loc11_.fps * (_loc11_.speed != 0?_loc11_.speed:1));
               if(_loc11_.repeatDelay)
               {
                  _loc13_.@repeatDelay = int(1000 / _loc11_.fps * _loc11_.repeatDelay);
               }
               if(_loc11_.swing)
               {
                  _loc13_.@swing = _loc11_.swing;
               }
               _loc13_.@frameCount = _loc12_;
               _loc14_ = <frames/>;
               _loc13_.appendChild(_loc14_);
               _loc15_ = 0;
               while(_loc15_ < _loc12_)
               {
                  _loc16_ = _loc11_.frameList[_loc15_];
                  _loc17_ = <frame/>;
                  _loc14_.appendChild(_loc17_);
                  _loc17_.@rect = _loc16_.rect.x + "," + _loc16_.rect.y + "," + _loc16_.rect.width + "," + _loc16_.rect.height;
                  if(_loc16_.delay)
                  {
                     _loc17_.@addDelay = int(1000 / _loc11_.fps * _loc16_.delay);
                  }
                  if(_loc16_.textureIndex != -1)
                  {
                     _loc18_ = _loc11_.textureList[_loc16_.textureIndex];
                     if(_loc18_.exportFrame != -1 && _loc18_.exportFrame != _loc15_)
                     {
                        _loc17_.@sprite = _loc18_.exportFrame;
                     }
                  }
                  _loc15_++;
               }
               publishData.outputDesc[_loc5_.id + ".xml"] = _loc13_;
               if(!publishData.usingAtlas)
               {
                  for each(_loc18_ in _loc11_.textureList)
                  {
                     if(_loc18_.exportFrame != -1 && _loc18_.raw)
                     {
                        publishData.outputRes[_loc5_.id + "_" + _loc18_.exportFrame + ".png"] = _loc18_.raw;
                     }
                  }
               }
            }
            else if(_loc5_.type == EPackageItem.SWF && publishData._project.type != EUIProject.TYPE_FLASH)
            {
               _loc9_ = null;
            }
            else
            {
               _loc7_ = UtilsFile.loadBytes(_loc5_.file);
               if(_loc7_)
               {
                  if(_loc5_.type == EPackageItem.FONT)
                  {
                     publishData.outputDesc[_loc6_] = _loc7_;
                  }
                  else
                  {
                     publishData.outputRes[_loc6_] = _loc7_;
                  }
                  _loc9_.@file = _loc6_;
               }
               else
               {
                  _loc9_ = null;
               }
            }
            if(_loc9_)
            {
               _loc2_.appendChild(_loc9_);
            }
         }
         if(publishData.usingAtlas)
         {
            for each(_loc19_ in publishData.atlasOutput)
            {
               _loc20_ = <atlas/>;
               _loc20_.@id = _loc19_.id;
               _loc20_.@size = _loc19_.width + "," + _loc19_.height;
               _loc20_.@file = _loc19_.fileName;
               _loc2_.appendChild(_loc20_);
               if(_loc19_.data)
               {
                  publishData.outputRes[_loc19_.fileName] = _loc19_.data;
               }
               if(_loc19_.alphaData)
               {
                  publishData.outputRes[UtilsStr.getFileName(_loc19_.fileName) + "!a.png"] = _loc19_.alphaData;
               }
            }
         }
         _loc1_.appendChild(_loc2_);
         publishData.outputDesc["package.xml"] = _loc1_;
         if(publishData._hitTestImages.length > 0 && !publishData.exportDescOnly)
         {
            this.handleHitTestImages();
         }
         else
         {
            stepCallback.callOnSuccess();
         }
      }
      
      private function handleHitTestImages() : void
      {
         var _loc1_:String = null;
         this._tasks = new BulkTasks(10);
         for each(_loc1_ in publishData._hitTestImages)
         {
            this._tasks.addTask(this.taskHandler,_loc1_);
         }
         this._tasks.start(stepCallback.callOnSuccess);
      }
      
      private function taskHandler() : void
      {
         var _loc1_:String = String(this._tasks.taskData);
         var _loc2_:EPackageItem = publishData.pkg.getItem(_loc1_);
         if(_loc2_)
         {
            publishData.pkg.getImage(_loc2_,this.onLoaded,true);
         }
         else
         {
            stepCallback.addMsg("cannot load hit test image: " + _loc1_);
            this._tasks.finishItem();
         }
      }
      
      private function onLoaded(param1:EPackageItem) : void
      {
         var _loc2_:BitmapData = BitmapData(param1.data);
         if(_loc2_ == null)
         {
            this._tasks.finishItem();
            return;
         }
         var _loc3_:BitmapData = new BitmapData(_loc2_.width / 2,_loc2_.height / 2,true,0);
         var _loc4_:Matrix = new Matrix();
         _loc4_.scale(0.5,0.5);
         _loc3_.draw(_loc2_,_loc4_);
         _loc2_ = _loc3_;
         var _loc5_:Vector.<uint> = _loc2_.getVector(_loc2_.rect);
         var _loc6_:int = _loc5_.length;
         publishData._hitTestData.writeUTF(param1.id);
         publishData._hitTestData.writeInt(0);
         publishData._hitTestData.writeInt(_loc2_.width);
         publishData._hitTestData.writeByte(2);
         publishData._hitTestData.writeInt(Math.ceil(_loc6_ / 8));
         _loc2_.dispose();
         var _loc7_:int = 0;
         var _loc8_:int = 0;
         var _loc9_:int = 0;
         while(_loc9_ < _loc6_)
         {
            if((_loc5_[_loc9_] >> 24 & 255) > 10)
            {
               _loc7_ = _loc7_ + (1 << _loc8_);
            }
            _loc8_++;
            if(_loc8_ == 8)
            {
               publishData._hitTestData.writeByte(_loc7_);
               _loc7_ = 0;
               _loc8_ = 0;
            }
            _loc9_++;
         }
         if(_loc8_ != 0)
         {
            publishData._hitTestData.writeByte(_loc7_);
         }
         this._tasks.finishItem();
      }
   }
}
